package org.Image.management.ViewService.login;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.Image.management.SpringDataService.Pojo.Result.Result;
import org.Image.management.SpringDataService.Service.Sys_User.Pojo.dto.LoginRequest;
import org.Image.management.SpringDataService.Service.Sys_User.Pojo.mapper.User;
import org.Image.management.SpringDataService.Service.Sys_User.controller.UserController;
import org.Image.management.ViewCofig.file.FxmlPath;
import org.Image.management.ViewController.MainViewApp;
import org.Image.management.ViewService.utils.ByPieceToolMethod;
import org.Image.management.ViewService.utils.informationalTips;
import org.Image.management.utils.UserIdThreadLocalStorage;
import org.springframework.stereotype.Controller;

import java.util.Objects;

import static org.Image.management.ViewService.utils.informationalTips.setPrompt;

@Slf4j
@Controller
@RequiredArgsConstructor
public class loginController {
    //依赖注入 用户服务类
    private final UserController userController;
    @FXML
    public Label prompt;

    @FXML
    public BorderPane setBackground;

    @FXML
    public CheckBox choose_displayPassword;

    @FXML
    public TextField display_password;

    @FXML
    public TextField Field_name;

    @FXML
    public PasswordField Field_password;


    @FXML
    private void initialize() {

    }

    //登录
    public void loginAction(ActionEvent actionEvent) {
        String inputName = getInputName();
        String inputPassword = getInputPassword();
//        log.info("用户名：{}，密码：{}", inputName, inputPassword);
        if (Objects.isNull(inputName) || Objects.isNull(inputPassword)) {
            return;
        }

        // 登录 需要链接数据库
        Result<?> login = userController.login(new LoginRequest(inputName, inputPassword));

        if (!login.isSuccess()) {
            log.error("登录失败{}", login.getMessage());
            setPrompt(prompt, login.getMessage());
            return;
        }

        if (login.getData() instanceof User data) {
            UserIdThreadLocalStorage.setUserToken(data.getId().toString());
            MainViewApp.setResizable.changeView(FxmlPath.Main, true, true);
        }

    }

    //注册
    public void enrollAction(ActionEvent actionEvent) {
        MainViewApp.setResizable.changeView(FxmlPath.Enroll, false, false);
    }

    //清除文本
    public void purgeText(ActionEvent actionEvent) {
        Field_name.setText("");
        Field_password.setText("");
        display_password.setText("");
    }

    //显示密码
    public void displayPassword(ActionEvent actionEvent) {
        boolean selected = choose_displayPassword.isSelected();
        // 批量更改按键的 “setVisible”（是否隐藏）
        ByPieceToolMethod.batchSetVisible(selected, display_password);
        ByPieceToolMethod.batchSetVisible(!selected, Field_password);
        if (selected) {
            display_password.setText(Field_password.getText());
        } else {
            Field_password.setText(display_password.getText());
        }
    }


    //获取输入的名称
    public String getInputName() {
        String text = Field_name.getText();
        /*
         *     @NotBlank(message = "用户名不能为空且不能只包含空格")
         *     @Size(min = 2, max = 20, message = "用户名长度需在2-20字符之间")
         * */
        if (text == null || text.isEmpty() || text.matches("\\s+")) {
            informationalTips.setPrompt(prompt, "请输入用户名并不能只包含空格");
            return null;
        }
        if (!text.matches(".{2,20}")) {
            informationalTips.setPrompt(prompt, "用户名长度需在2-20字符之间");
            return null;
        }

        return text;
    }

    //获取输入的密码
    public String getInputPassword() {
        String text = choose_displayPassword.isSelected() ? display_password.getText() : Field_password.getText();
        /*
         *@Pattern(regexp = "^(?=.*[A-Za-z])(?=.*\\d)[A-Za-z\\d]{8,}$",
         *       message = "密码必须至少8位，包含字母和数字")
         *@Size(min = 8, max = 20, message = "密码长度需在8-20字符之间")
         * */
        if (text == null || text.isEmpty() || text.matches("\\s+")) {
            informationalTips.setPrompt(prompt, "请输入密码并不能只包含空格");
            return null;
        }
        if (!text.matches(".{8,20}")) {
            informationalTips.setPrompt(prompt, "密码长度需在8-20字符之间");
            return null;
        }
        if (!text.matches("^(?=.*[A-Za-z])(?=.*\\d)[A-Za-z\\d]{8,}$")) {
            informationalTips.setPrompt(prompt, "密码必须至少8位，包含字母和数字");
            return null;
        }
        return text;
    }

}
